package com.haibara.aiojjudgeservice.strategy;

import cn.hutool.json.JSONUtil;
import com.haibara.aiojmodel.model.dto.question.JudgeCase;
import com.haibara.aiojmodel.model.dto.question.JudgeConfig;
import com.haibara.aiojmodel.model.dto.questionsubmit.JudgeInfo;
import com.haibara.aiojmodel.model.entity.Question;
import com.haibara.aiojmodel.model.enums.JudgeInfoMessageEnum;

import java.util.List;

public class JavaJudgeStrategy implements JudgeStrategy {

    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        Question question = judgeContext.getQuestion();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();

        JudgeInfo judgeInfoRespond = new JudgeInfo();
        Double memory = judgeInfo.getMemory();
        Long time = judgeInfo.getTime();
        judgeInfoRespond.setTime(time);
        judgeInfoRespond.setMemory(memory);

        // 5.根据沙箱执行结果，设置题目的判题状态和信息
        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;

        // a.判断inputList长度和outputList长度是否相等
        if(inputList.size() != outputList.size()){
            judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoRespond.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoRespond;
        }
        // b.判断outputList与预期输出是否一致
        for (int i = 0; i < outputList.size(); i++) {
            JudgeCase judgeCase = judgeCaseList.get(i);
            if(!judgeCase.getOutput().equals(outputList.get(i))){
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoRespond.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoRespond;
            }
        }
        // c.判断timeLimit和memoryLimit
        JudgeConfig bean = JSONUtil.toBean(question.getJudgeConfig(), JudgeConfig.class);
        Long memoryLimit = bean.getMemoryLimit();
        Long timeLimit = bean.getTimeLimit();
        Long JAVA_TIME = 100L;
        if(memory > memoryLimit){
            judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoRespond.setMemory(judgeContext.getJudgeInfo().getMemory());
            judgeInfoRespond.setTime(judgeContext.getJudgeInfo().getTime());
            judgeInfoRespond.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoRespond;
        }
        if((time - JAVA_TIME)  > timeLimit){
            judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoRespond.setMessage(judgeInfoMessageEnum.getValue());
            judgeInfoRespond.setMemory(judgeContext.getJudgeInfo().getMemory());
            judgeInfoRespond.setTime(judgeContext.getJudgeInfo().getTime());
            return judgeInfoRespond;
        }
        judgeInfoRespond.setMessage(judgeInfoMessageEnum.getValue());
        return judgeInfoRespond;
    }
}
